package com.witframework.web.tag.multicolumncombotag;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class MultiColumnComboTag extends BodyTagSupport{
  private static final String CUSTOMCOMBO = "mccstcmbtgcj2005";
  private String name = null;
  private String id = null;
  private String className = null;
  private String style = null;
  private String background = null;
  private String listBorderWidth = "1px";
  private String listBorderColor = "black";
  private String listWidth = "150px";
  private String listHeight = "80px";
  private String listBackground = null;
  private String listClassName = null;
  private String onChange = null;
  private boolean autoSubmit = false;
  private String option = null;
  private String value = null;
  private Collection collection = null;
  private Iterator iterator = null;
  private Enumeration jdField_enum = null;
  private Object[] ara = null;
  private int columns = 2;
  private boolean disabled = false;
  private Vector options = null;

  public void setName(String paramString)
  {
    this.name = paramString;
  }

  public String getName()
  {
    return this.name;
  }

  public void setId(String paramString)
  {
    this.id = paramString;
  }

  public String getId()
  {
    return this.id;
  }

  public void setClassName(String paramString)
  {
    this.className = paramString;
  }

  public String getClassName()
  {
    return this.className;
  }

  public void setStyle(String paramString)
  {
    this.style = paramString;
  }

  public String getStyle()
  {
    return this.style;
  }

  public void setColumns(int paramInt)
  {
    this.columns = paramInt;
  }

  public int getColumns()
  {
    return this.columns;
  }

  public void setListBorderWidth(String paramString)
  {
    this.listBorderWidth = paramString;
  }

  public String getListBorderWidth()
  {
    return this.listBorderWidth;
  }

  public void setListBorderColor(String paramString)
  {
    this.listBorderColor = paramString;
  }

  public String getListBorderColor()
  {
    return this.listBorderColor;
  }

  public void setListWidth(String paramString)
  {
    this.listWidth = paramString;
  }

  public String getListWidth()
  {
    return this.listWidth;
  }

  public void setListHeight(String paramString)
  {
    this.listHeight = paramString;
  }

  public String getListHeight()
  {
    return this.listHeight;
  }

  public void setListBackground(String paramString)
  {
    this.listBackground = paramString;
  }

  public String getListBackground()
  {
    return this.listBackground;
  }

  public void setListClassName(String paramString)
  {
    this.listClassName = paramString;
  }

  public String getListClassName()
  {
    return this.listClassName;
  }

  public void setOnChange(String paramString)
  {
    this.onChange = paramString;
  }

  public String getOnChange()
  {
    return this.onChange;
  }

  public void setDisabled(boolean paramBoolean)
  {
    this.disabled = paramBoolean;
  }

  public boolean getDisabled()
  {
    return this.disabled;
  }

  public void setAutoSubmit(boolean paramBoolean)
  {
    this.autoSubmit = paramBoolean;
  }

  public boolean getAutoSubmit()
  {
    return this.autoSubmit;
  }

  public void setOption(String paramString)
  {
    this.option = paramString;
  }

  public String getOption()
  {
    return this.option;
  }

  public void setValue(String paramString)
  {
    this.value = paramString;
  }

  public String getValue()
  {
    return this.value;
  }

  public void setSource(Object paramObject)
  {
    if (paramObject instanceof Vector)
    {
      this.jdField_enum = ((Vector)paramObject).elements();
    }
    else if (paramObject instanceof Enumeration)
    {
      this.jdField_enum = ((Enumeration)paramObject);
    }
    else if (paramObject instanceof Iterator)
    {
      this.iterator = ((Iterator)paramObject);
    }
    else if (paramObject instanceof Collection)
    {
      this.collection = ((Collection)paramObject);
      this.iterator = this.collection.iterator();
    }
    else
    {
      if (!(paramObject instanceof Object[]))
        return;
      this.ara = ((Object[])paramObject);
    }
  }

  public Object getSource()
  {
    if (this.jdField_enum != null)
      return this.jdField_enum;
    if (this.iterator != null)
      return this.iterator;
    return this.ara;
  }

  public void addElement(OptionBean paramOptionBean) {
    if (this.options == null)
      this.options = new Vector();   
    this.options.addElement(paramOptionBean);
  }

  public int doStartTag()
    throws JspException
  {
    if (this.ara != null)
    {
      for (int i = 0; i < this.ara.length; ++i)
        createOption(this.ara[i], this.option, this.value);
      this.ara = null;
    }
    else if (this.jdField_enum != null)
    {
      while (this.jdField_enum.hasMoreElements())
        createOption(this.jdField_enum.nextElement(), this.option, this.value);
      this.jdField_enum = null;
    }
    else if (this.iterator != null)
    {
      while (this.iterator.hasNext())
        createOption(this.iterator.next(), this.option, this.value);
      this.iterator = null;
    }
    return 2;
  }

  public int doAfterBody()
    throws JspException
  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent != null)
      localBodyContent.clearBody();
    return 0;
  }

  public int doEndTag()  throws JspException {
    StringBuffer localStringBuffer = new StringBuffer("");
    if (this.options == null)
      return 6;
    if (this.options.size() == 0)
      return 6;
    OptionBean localOptionBean1 = (OptionBean)this.options.elementAt(0);
    String str1 = localOptionBean1.getValue();
    String str2 = localOptionBean1.getText();
    for (int i = 1; i < this.options.size(); ++i) {
      localOptionBean1 = (OptionBean)this.options.elementAt(i);
      if (!(localOptionBean1.getSelected()))
        continue;
      str1 = localOptionBean1.getValue();
      str2 = localOptionBean1.getText();
    }
    Integer localInteger = (Integer)this.pageContext.getAttribute("mccstcmbtgcj2005", 1);
    int i = 0;
    int j = 0;
    if (localInteger != null)
      i = localInteger.intValue();
    if (this.id == null)
      this.id = "mccstcmbtgcj2005" + i;
    String str3 = this.id + "L";
    if (++i == 1)
      j = 1;
    this.pageContext.setAttribute("mccstcmbtgcj2005", new Integer(i), 1);
    localStringBuffer.append("<input type=\"hidden\" name=\"");
    localStringBuffer.append(this.name);
    localStringBuffer.append("\" value=\"");
    localStringBuffer.append(str1);
    localStringBuffer.append("\" id=\"");
    localStringBuffer.append(this.id + "0");
    localStringBuffer.append("\">\n");
    localStringBuffer.append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td valign=\"center\" onMouseOver=\"hideMComboMenu(getMComboElem('" + str3 + "'));\">\n");
    localStringBuffer.append("<input type=\"text\" id=\"");
    localStringBuffer.append(this.id);
    localStringBuffer.append("\"");
    if (this.className != null)
    {
      localStringBuffer.append(" class=\"");
      localStringBuffer.append(this.className);
      localStringBuffer.append("\"");
    }
    if (this.style != null)
    {
      localStringBuffer.append(" style=\"");
      localStringBuffer.append(this.style);
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" onFocus=\"blur()\"");
    localStringBuffer.append(" value=\"");
    localStringBuffer.append(str2);
    localStringBuffer.append("\"");
    localStringBuffer.append(">");
    localStringBuffer.append("</td><td valign=\"center\">\n");
    localStringBuffer.append("<button id=\"");
    localStringBuffer.append(this.id + "1");
    localStringBuffer.append("\"");
    if (this.className != null)
    {
      localStringBuffer.append(" class=\"");
      localStringBuffer.append(this.className);
      localStringBuffer.append("\"");
    }
    if (this.style != null)
    {
      localStringBuffer.append(" style=\"");
      localStringBuffer.append(this.style);
      localStringBuffer.append("\"");
    }
    if (!(this.disabled))
    {
      localStringBuffer.append(" onclick=\"popupMComboMenu(getMComboElem('");
      localStringBuffer.append(this.id);
      localStringBuffer.append("'),'");
      localStringBuffer.append(str3);
      localStringBuffer.append("');document.getElementById('"+str3+"').focus(); return false;\"");
    }
    else
    {
      localStringBuffer.append(" disabled");
    }
    localStringBuffer.append(" onMouseOut=\"hideMComboMenu(getMComboElem('" + str3 + "'));\" >");
    localStringBuffer.append("&darr;");
    localStringBuffer.append("</button>\n");
    localStringBuffer.append("</td></tr></table>\n");
    localStringBuffer.append("<div onMouseOut=\"hideMComboMenu(getMComboElem('" + str3 + "'));\" id=\"");
    localStringBuffer.append(str3);
    localStringBuffer.append("\"");
    if (this.listClassName != null)
    {
      localStringBuffer.append(" class=\"");
      localStringBuffer.append(this.listClassName);
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" style=\"top:0;left:0;position:absolute;z-index:200009;visibility:hidden;");
    localStringBuffer.append("width:");
    localStringBuffer.append(this.listWidth);
    localStringBuffer.append(";");
    localStringBuffer.append("height:");
    localStringBuffer.append(this.listHeight);
    localStringBuffer.append(";");
    localStringBuffer.append("cursor:pointer;border-style:solid;");
    localStringBuffer.append("border-width:");
    localStringBuffer.append(this.listBorderWidth);
    localStringBuffer.append(";");
    localStringBuffer.append("border-color:");
    localStringBuffer.append(this.listBorderColor);
    localStringBuffer.append(";");
    if (this.listBackground != null)
    {
      localStringBuffer.append("background-color:");
      localStringBuffer.append(this.listBackground);
      localStringBuffer.append(";");
    }
    localStringBuffer.append("overflow:auto;\" onMouseOver=\"showMComboMenu(this);\">\n");
    localStringBuffer.append("<table border=\"0\" width=\"100%\">\n");
    localStringBuffer.append("<tr>");
    int k = 1;
    for (i = 0; i < this.options.size(); ++i){
      OptionBean localOptionBean2 = (OptionBean)this.options.elementAt(i);
      if (k > this.columns){
        localStringBuffer.append("</tr><tr>");
        k = 1;
      }
      if (k <= this.columns) {
        localStringBuffer.append("<td");
        if (localOptionBean2.getClassName() != null)
        {
          localStringBuffer.append(" class=\"");
          localStringBuffer.append(localOptionBean2.getClassName());
          localStringBuffer.append("\"");
        }
        if (localOptionBean2.getStyle() != null)
        {
          localStringBuffer.append(" style=\"");
          localStringBuffer.append(this.style);
          localStringBuffer.append("\"");
        }
        if (localOptionBean2.getOnMouseOver() != null)
        {
          localStringBuffer.append(" onMouseOver=\"");
          localStringBuffer.append(localOptionBean2.getOnMouseOver());
          localStringBuffer.append("\"");
        }
        if (localOptionBean2.getOnMouseOut() != null)
        {
          localStringBuffer.append(" onMouseOut=\"");
          localStringBuffer.append(localOptionBean2.getOnMouseOut());
          localStringBuffer.append("\"");
        }
        localStringBuffer.append(">");
      }
      ++k;
      localStringBuffer.append("<a style=\"text-decoration:none;\" href=\"javascript:setMComboText('");
      localStringBuffer.append(this.id);
      localStringBuffer.append("','");
      localStringBuffer.append(prepareText(localOptionBean2.getValue()));
      localStringBuffer.append("','");
      localStringBuffer.append(prepareText(localOptionBean2.getText()));
      localStringBuffer.append("','");
      localStringBuffer.append(str3);
      localStringBuffer.append("')\"");
      if (localOptionBean2.getTitle() != null)
      {
        localStringBuffer.append(" title=\"");
        localStringBuffer.append(localOptionBean2.getTitle());
        localStringBuffer.append("\"");
      }
      localStringBuffer.append(">");
      localStringBuffer.append(localOptionBean2.getText());
      localStringBuffer.append("</a>\n");
      localStringBuffer.append("</td>");
    }
    while (k <= this.columns)
    {
      localStringBuffer.append("<td>&nbsp;</td>");
      ++k;
    }
    localStringBuffer.append("</tr>");
    localStringBuffer.append("</table>");
    localStringBuffer.append("</div>\n");
    if (j != 0)
      localStringBuffer.append(getJavaScript());
    try
    {
      this.pageContext.getOut().write(localStringBuffer.toString());
    }
    catch (Exception localException)
    {
      throw new JspException("Could not write data " + localException.toString());
    }
    dropData();
    return 6;
  }

  public void release()
  {
    dropData();
    super.release();
  }

  private void dropData()  {
    this.name = null;
    this.id = null;
    this.className = null;
    this.style = null;
    this.columns = 2;
    this.autoSubmit = false;
    this.listBorderWidth = "1px";
    this.listBorderColor = "black";
    this.listWidth = "150px";
    this.listHeight = "80px";
    this.listBackground = null;
    this.listClassName = null;
    this.onChange = null;
    this.disabled = false;
    this.options = null;
    this.option = null;
    this.value = null;
    this.collection = null;
    this.iterator = null;
    this.jdField_enum = null;
    this.ara = null;
  }

  private String getJavaScript() {
    StringBuffer localStringBuffer = new StringBuffer("");
    localStringBuffer.append("<script type=\"text/javascript\" language=\"JavaScript\">\n");
    localStringBuffer.append("function getMComboX(o) { var x=0; if(document.layers) x=o.pageX; else { while(eval(o)) { x+=o.offsetLeft; o=o.offsetParent; } } return x; };\n");
    localStringBuffer.append("function getMComboY(o) { var y=0; if(document.layers) y=o.pageY; else { while(eval(o)) { y+=o.offsetTop; o=o.offsetParent; } } return y; };\n");
    localStringBuffer.append("function getMComboElem(id){\n");
    localStringBuffer.append("if (document.getElementById) return document.getElementById(id);\n");
    localStringBuffer.append("else if (document.layers) return document.layers[id];\n");
    localStringBuffer.append("else if (document.all) return document.all[id];}\n");
    localStringBuffer.append("function hideMComboMenu(obj){\n");
    localStringBuffer.append("if(document.layers){ obj.visibility='hide';\n");   
    localStringBuffer.append("}else{ obj.style.visibility='hidden';");
    localStringBuffer.append("var s=document.getElementsByTagName(\"select\");");
    localStringBuffer.append("for(var j=0;j<s.length;j++){s[j].style.display=\"\";}");
    localStringBuffer.append("}} \n");
    localStringBuffer.append("function showMComboMenu(obj){\n");
    localStringBuffer.append("if (document.layers){ obj.visibility='show'\n");
    localStringBuffer.append("}else{ obj.style.visibility='visible';  \n");
    localStringBuffer.append("var s=document.getElementsByTagName(\"select\");");
    localStringBuffer.append("for(var j=0;j<s.length;j++){s[j].style.display=\"none\";}");
    localStringBuffer.append("}}");
    localStringBuffer.append("function popupMComboMenu(objButton, idMenu){\n");
    localStringBuffer.append("o=getMComboElem(idMenu);\n");
    localStringBuffer.append("var nTop = getMComboY(objButton);\n");
    localStringBuffer.append("if (objButton.offsetHeight) nTop+=objButton.offsetHeight;\n");
    localStringBuffer.append("var nLeft = getMComboX(objButton);\n");
    localStringBuffer.append("if (document.layers)\n");
    localStringBuffer.append("{ o.pageY=nTop+'px';\n");
    localStringBuffer.append("  o.pageX=nLeft+'px';}\n");
    localStringBuffer.append("else\n");
    localStringBuffer.append("{ o.style.top=nTop+'px';\n");
    localStringBuffer.append("  o.style.left = nLeft+'px'; }\n");
    localStringBuffer.append("showMComboMenu(o); }\n");
    localStringBuffer.append("function setMComboText(id,val,txt,imenu){\n");
    localStringBuffer.append("  o=getMComboElem(id+'0'); o.value=val;\n");
    localStringBuffer.append("  o=getMComboElem(id); o.value=txt;\n");
    localStringBuffer.append("  hideMComboMenu(getMComboElem(imenu)); ");
    if (this.onChange != null)
    {
      localStringBuffer.append(this.onChange);
      if (!(this.onChange.endsWith(";")))
        localStringBuffer.append(";");
    }
    if (this.autoSubmit)
      localStringBuffer.append(" submit();");
    localStringBuffer.append("} \n");
    localStringBuffer.append("</script>\n");
    return localStringBuffer.toString();
  }

  private String prepareText(String paramString)  {
    StringBuffer localStringBuffer = new StringBuffer("");
    for (int i = 0; i < paramString.length(); ++i)
    {
      char c = paramString.charAt(i);
      if (c == '\'')
        localStringBuffer.append("\\'");
      else if (c == '"')
        localStringBuffer.append("\\'");
      else
        localStringBuffer.append(c);
    }
    return localStringBuffer.toString();
  }

  private String getProperty(Object paramObject, String paramString)   throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
  {
    Object localObject = null;
    if (paramObject instanceof Map)
    {
      localObject = ((Map)paramObject).get(paramString);
    }
    else if (paramObject instanceof Hashtable)
    {
      localObject = ((Hashtable)paramObject).get(paramString);
    }
    else
    {
      Class localClass = paramObject.getClass();
      Class[] arrayOfClass = new Class[0];
      Object[] arrayOfObject = new Object[0];
      Method localMethod = localClass.getMethod(getMethodName(paramString), arrayOfClass);
      localObject = localMethod.invoke(paramObject, arrayOfObject);
    }
    if (localObject == null)
      return "";
    return "" + localObject;
  }

  private String getMethodName(String paramString)  {
    String str = paramString.substring(0, 1);
    return "get" + str.toUpperCase() + paramString.substring(1);
  }

  private void createOption(Object paramObject, String paramString1, String paramString2)
    throws JspException
  {
    String str1 = "" + paramObject;
    String str2 = null;
    if (paramString1 != null)
      try
      {
        str1 = getProperty(paramObject, paramString1);
      }
      catch (Exception localException1)
      {
        throw new JspException("Could not get option " + localException1.toString());
      }
    if (paramString2 != null)
      try
      {
        str2 = getProperty(paramObject, paramString2);
      }
      catch (Exception localException2)
      {
        throw new JspException("Could not get value " + localException2.toString());
      }
    OptionBean localOptionBean = new OptionBean();
    localOptionBean.setText(str1);
    localOptionBean.setValue(str2);
    addElement(localOptionBean);
  }
}